<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>我的直播间</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            overflow: hidden;
        }

        .gift {
            width: 800px;
            height: 100px;
            position: fixed;
            bottom: 10px;
            left: calc((100% - 800px) / 2);
            border-radius: 10px;
            /*来点阴影*/
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
            /*来点阴影*/
            background-color: rgba(255, 255, 255, 0.8);
        }

        .gift-png {
            width: 80px;
            height: 80px;
            margin-top: 10px;
            float: left;
            margin-left: 10px;
            cursor: pointer;
            background-size: 100% 100%;
            border-radius: 20px;
        }

        .hj {
            background-image: url("./img/hj.png");
        }

        .hj:hover {
            background-image: url("./img/hj-dh.webp");
        }

        .fj {
            background-image: url("./img/fj.png");
        }

        .fj:hover {
            background-image: url("./img/fj-dh.webp");
        }

        .move {
            width: 500px;
            height: 80px;
            line-height: 80px;
            background: linear-gradient(90deg, #c23aa3, #da4195, #ec4f87, #f8617a, #ff7670, #ff8b6a, #ffa068, #ffb56b);
            border-radius: 20px;
            font-size: 25px;
            font-weight: 800;
            position: fixed;
            right: -500px;
            top: 0;
            color: #ad9bc2;
        }

        .move span {
            float: left;
            width: 80%;
            display: flex;
            align-items: center;
        }

        .hj1 {
            float: left;
            height: 100%;
        }

        .fj1 {
            float: left;
            height: 100%;
        }
    </style>
</head>

<body>
    <div class="gift">
        <div class="gift-png hj"></div>
        <div class="gift-png fj"></div>
    </div>
</body>
<script src="./js/jquery.js"></script>
<script>
    const ws = new WebSocket("ws://localhost:8080/live/gift/榜一大哥");
    //ws四种状态
    ws.onopen = function () {
        console.log("连接成功");
    }
    ws.onclose = function () {
        console.log("连接关闭");
    }
    ws.onmessage = function (e) {
        var parse = JSON.parse(e.data);
        creat(parse.gift, parse.username);

    }
    ws.onerror = function (e) {
        console.log(e);
    }

    $(document).on("click", ".hj", function () {
        var gift = JSON.stringify({
            "username": "榜一大哥",
            "gift": "hj",
        })
        ws.send(gift);
    })

    $(document).on("click", ".fj", function () {
        var gift = JSON.stringify({
            "username": "榜一大哥",
            "gift": "fj",
        })
        ws.send(gift);
    })

    function creat(gift, msg) {
        var name;
        if (gift == "hj") {
            name = "火箭"
        }
        if (gift == "fj") {
            name = "飞机"
        }
        var newDiv = $('<div class="move"><img class="' + gift + '1" src="./img/' + gift + '.png"><span>' + msg + '送来大' + name + '</span></div>');
        move(newDiv);
    }

    function move(newDiv) {
        const hei = $(document).height() / 2;
        var randomNumber = Math.floor(Math.random() * (hei - 1 + 1)) + 1;
        // 设置新div元素的样式，宽度自适应，背景色为随机颜色
        newDiv.css({
            'top': randomNumber // 放置在顶部
        });

        // 将新div元素添加到body中
        $('body').append(newDiv);

        // 从右往左动画移动
        newDiv.animate({
            'left': '-500px'
        }, 9000, "linear", function () {
            // 动画结束后移除元素
            $(this).remove();
        });
    }

</script>

</html>